﻿using System;
using System.IO;
using AudioBookWS.ServiceUtil;
using AudioCore.Common;

namespace AudioBookWS
{
    public partial class LoadImage : System.Web.UI.Page
    {
        private const string IMAGE_TYPE = "y";
        private const string ITEM_ID = "id";
        private const string SCREENSHOT_ID = "s";

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            int moduleID = 0;
            int imageType = 0;
            int itemID = 0;
            string imageUrl = string.Empty;

            if (Request.QueryString[IMAGE_TYPE] != null
                && int.TryParse(Request.QueryString[IMAGE_TYPE].ToString(), out imageType)
                && Request.QueryString[ITEM_ID] != null
                && int.TryParse(Request.QueryString[ITEM_ID].ToString(), out itemID))
            {
                switch (imageType)
                {
                    // Image of a category
                    case (int)WSConstants.ContentType.Category:
                        imageUrl = GlobalParam.DataFolder + Constant.CATEGORY_FOLDER + itemID.ToString() + Constant.IMAGE_EXTENSION;
                        break;

                    // Image of an article
                    case (int)WSConstants.ContentType.Article:
                        {
                            imageUrl = GlobalParam.DataFolder + Constant.AUDIO_FOLDER + itemID.ToString() + "\\" + itemID.ToString() + Constant.IMAGE_EXTENSION;
                        }
                        break;
                }

                if (File.Exists(imageUrl))
                {
                    Response.Buffer = true;
                    Response.ContentType = "image/jpeg";
                    Response.WriteFile(imageUrl);
                    Response.Flush();
                }
            }
        }
    }
}
